import Vue from 'vue'

// import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

// import Element from 'element-ui'
// import './styles/element-variables.scss'

// import '@/styles/index.scss' // global css
import '@/assets/scss/woot.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './utils/error-log' // error log

import * as filters from './filters' // global filters

import VueI18n from 'vue-i18n'
import i18n from './i18n'
import Vuelidate from 'vuelidate'

import { getConfig } from '@/api/config'

import VueDOMPurifyHTML from 'vue-dompurify-html'
import { domPurifyConfig } from './yichat.web.share/helpers/HTMLSanitizer'

import VueFormulate from '@braid/vue-formulate'

import { isPhoneE164OrEmpty } from './yichat.web.share/helpers/Validators'

import ActionCableConnector from '@/helpers/actionCable'

import { getQueryObject } from './utils/index'

Vue.use(VueI18n)
Vue.use(Vuelidate)
Vue.use(VueDOMPurifyHTML, domPurifyConfig)
// Vue.config.lang = 'en'
// Object.keys(i18n).forEach(lang => {
//   Vue.locale(lang, i18n[lang])
// })

const i18nConfig = new VueI18n({
  locale: 'en',
  messages: i18n
})

Vue.use(VueFormulate, {
  rules: {
    isPhoneE164OrEmpty: ({ value }) => isPhoneE164OrEmpty(value)
  },
  classes: {
    outer: 'mb-4 wrapper',
    error: 'text-red-400 mt-2 text-xs font-medium'
  }
})

// Event Bus
window.bus = new Vue()
Vue.config.productionTip = false

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from '../mock'
// if (process.env.NODE_ENV === 'production') {
//   mockXHR()
// }

// Vue.use(Element, {
//   size: Cookies.get('size') || 'medium' // set element-ui default size
// })

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

const startupConfig = getQueryObject(location.hash)
console.log(startupConfig)

getConfig({
  websiteToken: startupConfig.websiteToken,
  accountId: startupConfig.accountId
}).then(res => {
  const authToken = res.data.authToken

  window.chatwootWebChannel = res.data.webChannel
  window.authToken = authToken

  window.WIDGET = new Vue({
    el: '#app',
    router,
    store,
    i18n: i18nConfig,
    render: h => h(App)
  })

  window.actionCable = new ActionCableConnector(
    window.WIDGET,
    window.authToken,
    `${process.env.VUE_APP_BASE_API}/signalr-hubs/chat?type=contact`
  )
})
